/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-02 16:25:10
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-09 16:25:17
 */

import { useDispatch, useSelector } from 'react-redux'
import { useEffect } from 'react'
import { RootState } from '@/types/store'

// export const useInitialState = (action: () => void, actionState: any) => {
export function useInitialState<T extends keyof RootState>(action: () => void, actionState: T) {
  /* 
  action就是函数   getUserProfileAction
  actionState：获取redux中的数据 profile |  user
  */
  const dispatch = useDispatch()
  const state = useSelector((state: RootState) => state[actionState])
  useEffect(() => {
    dispatch(action())
  }, [action, dispatch])

  return state
}